<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Sending Multiple Mails per SMTP Connection - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.mail.multiple-emails.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.mail.multiple-emails.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.mail.sending.html">Sending via SMTP</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.mail.html">Zend_Mail</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.mail.different-transports.html">Using Different Transports</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.mail.multiple-emails" class="section"><div class="info"><h1 class="title">Sending Multiple Mails per SMTP Connection</h1></div>
    

    <p class="para">
        By default, a single SMTP transport creates a single connection and
        re-uses it for the lifetime of the script execution. You may send multiple
        e-mails through this SMTP connection. A RSET command is issued before each
        delivery to ensure the correct SMTP handshake is followed.
    </p>

    <p class="para">
        Optionally, you can also define a default From email address and name,
        as well as a default reply-to header. This can be done through the static
        methods  <span class="methodname">setDefaultFrom()</span> and
         <span class="methodname">setDefaultReplyTo()</span>. These defaults will be used when you
        don&#039;t specify a From/Reply-to Address or -Name until the defaults are reset (cleared).
        Resetting the defaults can be done through the use of the
         <span class="methodname">clearDefaultFrom()</span> and
         <span class="methodname">clearDefaultReplyTo</span>.
    </p>

    <div class="example" id="zend.mail.multiple-emails.example-1"><div class="info"><p><b>Example #1 Sending Multiple Mails per SMTP Connection</b></p></div>
        

        <pre class="programlisting brush: php">
// Create transport
$config = array(&#039;name&#039; =&gt; &#039;sender.example.com&#039;);
$transport = new Zend_Mail_Transport_Smtp(&#039;mail.example.com&#039;, $config);

// Set From &amp; Reply-To address and name for all emails to send.
Zend_Mail::setDefaultFrom(&#039;sender@example.com&#039;, &#039;John Doe&#039;);
Zend_Mail::setDefaultReplyTo(&#039;replyto@example.com&#039;,&#039;Jane Doe&#039;);

// Loop through messages
for ($i = 0; $i &lt; 5; $i++) {
    $mail = new Zend_Mail();
    $mail-&gt;addTo(&#039;studio@example.com&#039;, &#039;Test&#039;);

    $mail-&gt;setSubject(
        &#039;Demonstration - Sending Multiple Mails per SMTP Connection&#039;
    );
    $mail-&gt;setBodyText(&#039;...Your message here...&#039;);
    $mail-&gt;send($transport);
}

// Reset defaults
Zend_Mail::clearDefaultFrom();
Zend_Mail::clearDefaultReplyTo();
</pre>

    </div>

    <p class="para">
        If you wish to have a separate connection for each mail
        delivery, you will need to create and destroy your transport before and
        after each  <span class="methodname">send()</span> method is called. Or alternatively,
        you can manipulate the connection between each delivery by accessing the
        transport&#039;s protocol object.
    </p>

    <div class="example" id="zend.mail.multiple-emails.example-2"><div class="info"><p><b>Example #2 Manually controlling the transport connection</b></p></div>
        

        <pre class="programlisting brush: php">
// Create transport
$transport = new Zend_Mail_Transport_Smtp();

$protocol = new Zend_Mail_Protocol_Smtp(&#039;mail.example.com&#039;);
$protocol-&gt;connect();
$protocol-&gt;helo(&#039;sender.example.com&#039;);

$transport-&gt;setConnection($protocol);

// Loop through messages
for ($i = 0; $i &lt; 5; $i++) {
    $mail = new Zend_Mail();
    $mail-&gt;addTo(&#039;studio@example.com&#039;, &#039;Test&#039;);
    $mail-&gt;setFrom(&#039;studio@example.com&#039;, &#039;Test&#039;);
    $mail-&gt;setSubject(
        &#039;Demonstration - Sending Multiple Mails per SMTP Connection&#039;
    );
    $mail-&gt;setBodyText(&#039;...Your message here...&#039;);

    // Manually control the connection
    $protocol-&gt;rset();
    $mail-&gt;send($transport);
}

$protocol-&gt;quit();
$protocol-&gt;disconnect();
</pre>

    </div>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.mail.sending.html">Sending via SMTP</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.mail.html">Zend_Mail</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.mail.different-transports.html">Using Different Transports</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="reference.html">Refer&ecirc;ncia do Zend Framework</a></li>
  <li class="header up"><a href="zend.mail.html">Zend_Mail</a></li>
  <li><a href="zend.mail.introduction.html">Introduction</a></li>
  <li><a href="zend.mail.sending.html">Sending via SMTP</a></li>
  <li class="active"><a href="zend.mail.multiple-emails.html">Sending Multiple Mails per SMTP Connection</a></li>
  <li><a href="zend.mail.different-transports.html">Using Different Transports</a></li>
  <li><a href="zend.mail.html-mails.html">HTML E-Mail</a></li>
  <li><a href="zend.mail.attachments.html">Attachments</a></li>
  <li><a href="zend.mail.adding-recipients.html">Adding Recipients</a></li>
  <li><a href="zend.mail.boundary.html">Controlling the MIME Boundary</a></li>
  <li><a href="zend.mail.additional-headers.html">Additional Headers</a></li>
  <li><a href="zend.mail.character-sets.html">Character Sets</a></li>
  <li><a href="zend.mail.encoding.html">Encoding</a></li>
  <li><a href="zend.mail.smtp-authentication.html">SMTP Authentication</a></li>
  <li><a href="zend.mail.smtp-secure.html">Securing SMTP Transport</a></li>
  <li><a href="zend.mail.read.html">Reading Mail Messages</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>